package com.mingyizhudao.app.view;

import java.util.ArrayList;

import com.mingyizhudao.app.R;
import com.mingyizhudao.app.utils.CommonUtils;
import com.mingyizhudao.app.utils.IntentHelper;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class GuideActivity extends MYBaseActivity {
	/**装分页显示的view的数组*/
    private ArrayList<View> pageViews;  
    private ViewPager vPager;
    private ImageView iv_1,iv_2,iv_3;
    private TextView tv_go;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		initView();
	}

	private void initView() {
		  //将要分页显示的View装入数组中
        LayoutInflater inflater = getLayoutInflater();
        pageViews = new ArrayList<View>();
        int[] imageViews = {R.drawable.guide1,R.drawable.guide2,R.drawable.guide3};
        for( int i = 0; i < imageViews.length; i++ )
		{
        	View view = inflater.inflate(R.layout.vp_guide_view, null);
        	ImageView iv_guide = (ImageView)view.findViewById(R.id.iv_guide);
        	iv_guide.setImageResource( imageViews[i] );
        	
        	pageViews.add(view);
		}
        vPager = ( ViewPager )findViewById(R.id.vPager);
        
        iv_1 = (ImageView)findViewById(R.id.iv_1); 
    	iv_2 = (ImageView)findViewById(R.id.iv_2); 
    	iv_3 = (ImageView)findViewById(R.id.iv_3); 
    	tv_go = (TextView) findViewById(R.id.tv_go);
    	
    	tv_go.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick( View v )
			{
				IntentHelper.getInstance(activity).gotoActivity(MainActivity.class);
				CommonUtils.setIsShowedGuide(activity, "1");
				finish();
			}
		}); 
    	
        GuidePageAdapter  guidePageAdapter = new GuidePageAdapter();
        vPager.setAdapter(guidePageAdapter);
       
        vPager.addOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				
				
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				if (arg0 == 0) {
	        		iv_1.setImageResource(R.drawable.dot_green);
	        		iv_2.setImageResource(R.drawable.dot_gray);
	        		iv_3.setImageResource(R.drawable.dot_gray);
	        		
				}else if (arg0 == 1) {
					
					iv_1.setImageResource(R.drawable.dot_gray);
	        		iv_2.setImageResource(R.drawable.dot_green);
	        		iv_3.setImageResource(R.drawable.dot_gray);
	        		
				}else {
					iv_1.setImageResource(R.drawable.dot_gray);
	        		iv_2.setImageResource(R.drawable.dot_gray);
	        		iv_3.setImageResource(R.drawable.dot_green);
				}
	        	
	        	// 测试页卡2内的按钮事件  
				if (arg0 == 2) {  
					tv_go.setVisibility( View.VISIBLE );
					 
				}else {
					tv_go.setVisibility( View.INVISIBLE );
				}  
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
        
	}

	@Override
	public void initHeaderView() {
	}

	class GuidePageAdapter extends PagerAdapter{
		//销毁position位置的界面
		@Override
		public void destroyItem(View v, int position, Object arg2) {

			((ViewPager)v).removeView(pageViews.get(position));
		}
		@Override
		public void finishUpdate(View arg0) {

		}
		//获取当前窗体界面数
		@Override
		public int getCount() {

			return pageViews.size();
		}

		//初始化position位置的界面
		@Override
		public Object instantiateItem(View v, int position) {
			// TODO Auto-generated method stub
			((ViewPager) v).addView(pageViews.get(position));  
			
			return pageViews.get(position);  
		}


		// 判断是否由对象生成界面
		@Override
		public boolean isViewFromObject(View v, Object arg1) {
			// TODO Auto-generated method stub
			return v == arg1;
		}

		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}

	}
}
